package com.ling.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Msg;
import com.ling.bean.StarBlog;
import com.ling.bean.User;
import com.ling.service.StarBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lin
 */
@Controller
public class StarBlogController {
    private final StarBlogService starBlogService;

    @Autowired
    public StarBlogController(StarBlogService starBlogService) {
        this.starBlogService = starBlogService;
    }

    //获取用户收藏的文章，分页
    @GetMapping("/getUserStarBlogs/{pn}")
    @ResponseBody
    public Msg getUserStarBlogs(HttpSession httpSession, @PathVariable Integer pn) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登陆！！");
        }
        PageHelper.startPage(pn, 15);
        List<StarBlog> userStarBlogs= starBlogService.getUserStarBlogsByUserId(user.getId());
        PageInfo<StarBlog> pageInfo = new PageInfo<>(userStarBlogs);
        return Msg.success().add("pageInfo",pageInfo);
    }

    //取消收藏文章
    @GetMapping("/cancelStarBlog/{starBlogId}")
    @ResponseBody
    public Msg cancelStarBlog(HttpSession httpSession, @PathVariable String starBlogId) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登陆！！");
        }
        starBlogService.cancelStarBlog(user.getId(),starBlogId);
        return Msg.success();
    }

}
